const app = getApp()
const api = require('../../../api/index.js')
var util = require("../../../utils/util.js");
const tokenState = getApp().messages();
import templates from '../commentList/commentList'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [],
    note_id: '',
    address: [{
        img: '/pages/img / cooperation.png',
        name: '张三',
        nameData: '楼主',
        text: '骑马好真好特别好',
        time: '2天前',
        id: '1'
      },
      {
        img: '/pages/img / cooperation.png',
        name: '张三3',
        nameData: '',
        text: '骑马好真好特别好',
        time: '4天前',
        id: '2'
      },
      {
        img: '/pages/img / cooperation.png',
        name: '张三3',
        nameData: '',
        text: '骑马好真好特别好',
        time: '3天前',
        id: '3'
      },

      {
        img: '/pages/img / cooperation.png',
        name: '张三4',
        nameData: '',
        text: '骑马好真好特别好4',
        time: '4天前',
        id: '4'
      },

      {
        img: '/pages/img / cooperation.png',
        name: '张三5',
        nameData: '',
        text: '骑马好真好特别5',
        time: '5天前',
        id: '5'
      },

      {
        img: '/pages/img / cooperation.png',
        name: '张三6',
        nameData: '',
        text: '骑马好真好特别好6',
        time: '6天前',
        id: '6'
      },

      {
        img: '/pages/img / cooperation.png',
        name: '张三7',
        nameData: '',
        text: '骑马好真好特别好7',
        time: '7天前',
        id: '7'
      },
    ],
    imgSrc: 'https://www.dicard.cn/Public/',
    num: 90,
    indicatorDots: false,
    autoplay: false,
    interval: 5000,
    duration: 1000,
    imgNum: 0,
    hidden: false,
    //是否采用衔接滑动
    circular: true,
    //是否显示画板指示点
    indicatorDots: false,
    //选中点的颜色
    indicatorcolor: "#000",
    //是否竖直
    vertical: false,
    //是否自动切换
    autoplay: false,
    //滑动动画时长毫秒
    duration: 100,
    //所有图片的高度
    imgheights: [],
    //图片宽度
    imgwidth: 750,
    //默认
    current: 0,
    tokenState: false,
    removeData: false

  },
  swiperChange: function(e) {
    var that = this;
    that.setData({
      imgNum: e.detail.current
    })
  },
  imageLoad: function(e) {
    //获取图片真实宽度
    var imgwidth = e.detail.width,
      imgheight = e.detail.height,
      //宽高比
      ratio = imgwidth / imgheight;

    //计算的高度值
    var viewHeight = 750 / ratio;
    var imgheight = viewHeight
    var imgheights = this.data.imgheights
    console.log(imgheights)
    //把每一张图片的高度记录到数组里
    imgheights.push(imgheight)
    this.setData({
      imgheights: imgheights,
    })
  },
  bindchange: function(e) {
    console.log(e.detail.current, 789)
    this.setData({
      current: e.detail.current
    })
  },
  itemclick(event) {
    templates.Delete(event)
  },
  Delete: function(e) {
    let myCYID = e.currentTarget.dataset.id
    console.log(e)
    this.setData({
      setId: myCYID,
      removeData: true
    })
  },
  DeleteR: function() {
    let comment_id = this.data.setId
    let data = {
      comment_id: comment_id
    }
    let that = this
    api.commentRemove(data, (res) => {
      if (res.data.code == 200) {
        let data = {
          note_id: this.data.note_id,
          latitude: wx.getStorageSync('key').latitude,
          longitude: wx.getStorageSync('key').longitude
        }
        api.communityActive(data, (res) => {
          let comment = `activeData.comment`
          this.setData({
            activeData: res.data.result,
            hidden: true,
            removeData: false,
            note_id: this.data.note_id,
            [comment]: res.data.result.comment
          })
          wx.showToast({
            title: '删除成功',
            duration: 1000
          });
        })


      } else {
        wx.showToast({
          title: '删除失败稍后再试试哦',
          duration: 1000
        });
      }

    })
  },
  cancel: function() {
    this.setData({
      removeData: false
    })
  },
  viewData: function() {
    let that = this
    let datatoken = this.token()
    let note_id = this.data.note_id
    let conten = this.data.activeData.count
    if (datatoken === 1) {
      wx.navigateTo({
        url: `/pages/community/Comments/Comments?id=${note_id}&conten=${conten}`
      })
    }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    this.setData({
      imgSrc: api.imgPathUrl
    })
    this.setData({
      note_id: options.note_id
    })
    let data = {
      note_id: options.note_id,
      latitude: wx.getStorageSync('key').latitude,
      longitude: wx.getStorageSync('key').longitude
    }
    this.active(data)
  },
  active: function(data) {

    api.communityActive(data, (res) => {
      console.log(res)
      this.setData({
        activeData: res.data.result,
        hidden: true
      })

    })
  },
  save: function() {
    let that = this
    let datatoken = this.token()
    console.log(datatoken)
    if (datatoken === 1) {
      let dataNum = this.data.activeData.agree_num
      console.log(dataNum)
      dataNum++;
      let num = `activeData.agree_num`
      if (dataNum <= 10000) {
        that.setData({
          [num]: dataNum
        })
      } else {
        that.setData({
          [num]: '1W+'
        })
      }
    }
    let data = {
      note_id: this.data.note_id
    }
    setTimeout(() => {
      api.like(data, (res) => {
        console.log(res)
      })
    }, 3000)
  },

  btnText: function(e) {
    let that = this
    let datatoken = this.token()
    if (datatoken === 1) {
      let ids = this.data.activeData.note_id
      let val = e.detail.value
      let data = {
        note_id: ids,
        name: val
      }
      let thata = this
      if (val.length != '') {
        this.setData({
          hidden: false
        })
        api.comment(data, (res) => {
          console.log(res)
          this.setData({
            searchinput: '',
          })
          let ac = `activeData.comment`
          let act = `activeData.count`
          let data = {
            note_id: thata.data.note_id,
            latitude: wx.getStorageSync('key').latitude,
            longitude: wx.getStorageSync('key').longitude
          }
          api.communityActive(data, (res) => {
            this.setData({
              [ac]: res.data.result.comment,
              [act]: res.data.result.count,
              hidden: true
            })
          })
        })
      }
    }
  },
  token: function() {
    let tokenState = getApp().messages();
    let that = this
    if (tokenState === 0) {
      that.setData({
        tokenState: true
      })
      return 0
    } else {
      return 1
    }
  },
  getPhoneNumber: function(e) {
    let sessionKeyS = wx.getStorageSync('session_key').session_key;
    console.log(sessionKeyS);
    if (sessionKeyS == '' || sessionKeyS == undefined) {
      app.Logins();
    }
    let invite_id = wx.getStorageSync('invite_id');
    let data = {
      encryptedData: e.detail.encryptedData,
      iv: e.detail.iv,
      sessionKey: sessionKeyS,
      invite_id: invite_id
    }
    console.log(data);
    api.getPhoneNumber(data, (res) => {
      if (res.data.code == 50003) {
        this.setData({
          tokenState: true
        })
        wx.showToast({
          title: '亲，登录需要授权哦~',
          icon: 'none',
          duration: 2000
        })
      } else {
        this.setData({
          userData: res.data.result,
          tokenState: false
        })
        app.globalData.Refresh = true;
        wx.setStorageSync('token', {
          "token": res.data.result.token,
          "user_mobile": res.data.result.user_mobile
        });
        console.log(wx.getStorageSync('token'))
      }

    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    wx.setStorageSync('active',{
      active: 1,
    }) 
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})